package apiday01;

import java.util.Locale;
import java.util.Random;
import java.util.Scanner;

/**
 * 生成一个4位验证码(数字和字母的组合)，输出到控制台并提示用户输入验证码，
 * 输入后若正确则提示验证码正确，若错误则提示验证码错误。注意：不区分大小写
 */
public class ApiTest {
    public static void main(String[] args) {
       String str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRETUVWXYZ";
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        for (;;){
            String code = "";
            for (int i = 0;i<4;i++){
                int index = random.nextInt(str.length());
                char c =str.charAt(index);
                code += c;
            }
            System.out.println(code);
            code=code.toUpperCase(Locale.ROOT);
            System.out.println("请输入验证码");
            String input = scanner.nextLine();
            input = input.toUpperCase(Locale.ROOT);
            if (input.equals(code)){
                System.out.println("输入正确");
                return;
            }else {
                System.out.println("输入错误");
            }
        }

















        /*String str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        String code = "";
        for (int i = 0;i<4;i++){
            int index = random.nextInt(str.length());
            char c = str.charAt(index);
            code += c;
        }
        code = code.toLowerCase(Locale.ROOT);
        System.out.println("验证码："+code);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入上面验证码");
        String input = scanner.nextLine();
        input = input.toLowerCase();
        if (input.equals(code)){
            System.out.println("验证通过");
        }else {
            System.out.println("验证失败");
        }

         */

    }
}
